using Google.ProtocolBuffers;
using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Network;
using System;
using System.Reflection;
namespace MicroMsgBackground.Scene
{
	public abstract class NetSceneBaseEx<TRequest, TResponse, TBuilder> : NetSceneBase where TRequest : AbstractMessageLite<TRequest, TBuilder> where TBuilder : AbstractBuilderLite<TRequest, TBuilder>
	{
		private const string TAG = "NetSceneBaseEx";
		protected TBuilder mBuilder;
		protected PackResult mErrorCode;
		protected bool beginBuilder()
		{
			try
			{
				Type typeFromHandle = typeof(TRequest);
				this.mBuilder = (TBuilder)((object)typeFromHandle.InvokeMember("CreateBuilder", 282, null, null, null));
				if (this.mBuilder != null)
				{
					this.mSessionPack = new SessionPack();
					return true;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("NetSceneBaseEx", "initBuilder exception" + ex.ToString());
			}
			return false;
		}
		protected bool endBuilder()
		{
			this.mSessionPack.mRequestObject = this.mBuilder.Build();
			this.mSessionPack.mProcRequestToByteArray += new RequestToByteArrayDelegate(this.requestToByteArray);
			this.mSessionPack.mResponseParser += new OnResponseParserDelegate(this.onParserResponse);
			this.mSessionPack.mCompleted += new SessionPackCompletedDelegate(this.onCompleted);
			bool result = Sender.getInstance().sendPack(this.mSessionPack);
			this.mBuilder = default(TBuilder);
			return result;
		}
		private byte[] requestToByteArray(object object_0)
		{
			return ((AbstractMessageLite<TRequest, TBuilder>)object_0).ToByteArray();
		}
		private object onParserResponse(SessionPack sessionPack)
		{
			try
			{
				Type typeFromHandle = typeof(TResponse);
				return typeFromHandle.InvokeMember("ParseFrom", 282, null, null, new object[]
				{
					sessionPack.mResponseBuffer
				});
			}
			catch (TargetInvocationException ex)
			{
				Log.smethod_1("NetSceneBaseEx", "onParserResponse exception" + ex.get_Message());
				sessionPack.mCompleted -= new SessionPackCompletedDelegate(this.onCompleted);
			}
			catch (Exception ex2)
			{
				Log.smethod_1("NetSceneBaseEx", "onParserResponse exception" + ex2.ToString());
			}
			return null;
		}
		private void onCompleted(object sender, PackEventArgs e)
		{
			SessionPack sessionPack = sender as SessionPack;
			TResponse response = (TResponse)((object)sessionPack.mResponseObject);
			TRequest request = (TRequest)((object)sessionPack.mRequestObject);
			if (!e.isSuccess())
			{
				Log.smethod_1("NetSceneBaseEx", "send pack error, result = " + e.result);
				this.mErrorCode = e.result;
				this.mSessionPack = null;
				this.onFailed(request, response);
				return;
			}
			if (this.mSessionPack == null)
			{
				Log.smethod_1("NetSceneBaseEx", "response on success, but cancelled, seq = " + sessionPack.mSeqID);
				this.mErrorCode = PackResult.BEEN_CANCELLED;
				this.onFailed(request, response);
				return;
			}
			this.mSessionPack = null;
			this.onSuccess(request, response);
		}
		protected PackResult getLastError()
		{
			return this.mErrorCode;
		}
		protected abstract void onFailed(TRequest request, TResponse response);
		protected abstract void onSuccess(TRequest request, TResponse response);
		public bool check(string args)
		{
			return args != null && args.get_Length() > 0;
		}
	}
}
